package cn.jflow.boot.config;

import java.io.File;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import bp.difference.SystemConfig;
import cn.jflow.boot.web.UserContextInterceptor;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
    /**
     * 静态资源处理
     **/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**") // 你可以自定义路径
                .addResourceLocations("classpath:/META-INF/resources/"); // 自定义静态资源目录

        if(SystemConfig.isJarRun()==false)
            return;
        String webPath = SystemConfig.getPhysicalPath();

        File file = new File(webPath + "DataUser/Siganture/");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/UploadFile/");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/CyclostyleFile");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/TempleteOfExp");
        if (!file.exists()) {
            file.mkdirs();
        }

        file = new File(webPath+"DataUser/FlowDesc");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/RichTextFile");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/Temp");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/JSLibData");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/Style/TemplateFoolDevelopDesigner");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/Bill");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/HandWritingImg");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/UserIcon");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/TempleteOfImp");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/ThirdpartySoftware");
        if (!file.exists()) {
            file.mkdirs();
        }
        file = new File(webPath+"DataUser/ImgAth");
        if (!file.exists()) {
            file.mkdirs();
        }


        file = new File(webPath+"DataUser/TempleteTraining");
        if (!file.exists()) {
            file.mkdirs();
        }

        file = new File(webPath+"DataUser/TempleteAfterSale");
        if (!file.exists()) {
            file.mkdirs();
        }

        System.out.println("静态资源处理：" + webPath + "/DataUser/");
        registry.addResourceHandler("/DataUser/Siganture/**").addResourceLocations("file:" + webPath + "/DataUser/Siganture/");
        registry.addResourceHandler("/DataUser/UploadFile/**").addResourceLocations("file:" + webPath + "/DataUser/UploadFile/");
        registry.addResourceHandler("/DataUser/CyclostyleFile/**").addResourceLocations("file:" + webPath + "/DataUser/CyclostyleFile/");
        registry.addResourceHandler("/DataUser/TempleteOfExp/**").addResourceLocations("file:" + webPath + "/DataUser/CyclostyleFile/");
        registry.addResourceHandler("/DataUser/FlowDesc/**").addResourceLocations("file:" + webPath + "/DataUser/FlowDesc/");
        registry.addResourceHandler("/DataUser/InstancePacketOfData/**").addResourceLocations("file:" + webPath + "/DataUser/InstancePacketOfData/");
        registry.addResourceHandler("/DataUser/Temp/**").addResourceLocations("file:" + webPath + "/DataUser/Temp/");
        registry.addResourceHandler("/DataUser/JSLibData/**").addResourceLocations("file:" + webPath + "/DataUser/JSLibData/");
        registry.addResourceHandler("/DataUser/Style/TemplateFoolDevelopDesigner/**").addResourceLocations("file:" + webPath + "/DataUser/Style/TemplateFoolDevelopDesigner/");
        registry.addResourceHandler("/DataUser/Bill/**").addResourceLocations("file:" + webPath + "/DataUser/Bill/");
        registry.addResourceHandler("/DataUser/HandWritingImg/**").addResourceLocations("file:" + webPath + "/DataUser/HandWritingImg/");
        registry.addResourceHandler("/DataUser/UserIcon/**").addResourceLocations("file:" + webPath + "/DataUser/UserIcon/");
        registry.addResourceHandler("/DataUser/TempleteOfImp/**").addResourceLocations("file:" + webPath + "/DataUser/TempleteOfImp/");
        registry.addResourceHandler("/DataUser/ThirdpartySoftware/**").addResourceLocations("file:" + webPath + "/DataUser/ThirdpartySoftware/");
        registry.addResourceHandler("/DataUser/ImgAth/**").addResourceLocations("file:" + webPath + "/DataUser/ImgAth/");

        registry.addResourceHandler("/DataUser/TempleteTraining/**").addResourceLocations("file:" + webPath + "/DataUser/TempleteTraining/");
        registry.addResourceHandler("/DataUser/TempleteAfterSale/**").addResourceLocations("file:" + webPath + "/DataUser/TempleteAfterSale/");



    }

    /**
     * 跨域设置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*") // 可以设置允许的地址例如："http://localhost:3000", "http://localhost:8080"
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(false); //禁用凭证（如果不需 cookies/认证）
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserContextInterceptor()).addPathPatterns("/**");
    }

    //防止输出汉字乱码
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        converters.add(converter);
    }
    //防止输出汉字乱码
    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        return new StringHttpMessageConverter(StandardCharsets.UTF_8);
    }
}

